package org.o14x.alpha.ui;

import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.contexts.IContextService;
import org.o14x.alpha.domain.Clipboard;
import org.o14x.alpha.ui.controller.UIController;
import org.o14x.alpha.ui.coolbar.AddressBarContributionItem;

/**
 * Application's ApplicationWorkbenchWindowAdvisor.
 * 
 * @author Olivier DANGREAUX
 */
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	/**
	 * UIController.
	 */
	private UIController uiController;

	/**
	 * Creates a new instance of ApplicationWorkbenchWindowAdvisor.
	 *
	 * @param configurer A IWorkbenchWindowConfigurer.
	 * @param uiController The UIController;
	 */
	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer, UIController uiController) {
		super(configurer);
		this.uiController = uiController;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
	 */
	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	/**
	 * {@inheritDoc}
	 */
	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(800, 600));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowCreate()
	 */
	@Override
	public void postWindowCreate() {
		PreferenceManager pm = getWindowConfigurer().getWindow().getWorkbench().getPreferenceManager();
		pm.remove("org.eclipse.ui.preferencePages.Workbench");
		pm.remove("org.eclipse.update.internal.ui.preferences.MainPreferencePage");

		ICoolBarManager coolBar = getWindowConfigurer().getActionBarConfigurer().getCoolBarManager();
		uiController.getUi().setCoolBar(coolBar);

		String[] items = new String[] { "org.eclipse.ui.edit.text.actionSet.navigation", "org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo", "org.eclipse.ui.edit.text.actionSet.annotationNavigation" };

		for (int i = 0; i < items.length; i++) {
			coolBar.remove(items[i]);
		}

		AddressBarContributionItem addressBarContributionItem = new AddressBarContributionItem(uiController);
		coolBar.add(new Separator());
		coolBar.add(addressBarContributionItem);

		coolBar.update(true);

		addressBarContributionItem.update();

		IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
		contextService.activateContext("org.o14x.alpha.ui.contexts.window");
		
		uiController.getDomain().setClipboard(new Clipboard());

	}
}
